<?php

namespace app\admin\controller\share;

use app\common\controller\Backend;
use think\Db;
use linghuo\Linghuo;


/**
 * 用户收益统计
 *
 * @icon fa fa-circle-o
 */
class Goodsreward extends Backend
{

    /**
     * UserWithdraw模型对象
     * @var \app\admin\model\UserWithdraw
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

    }
    
    public function index(){
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $offset = $this->request->post("offset"); // 第几页
            $offset = empty( $offset) ? 0 :  bcsub($offset,1);
            $limit = $this->request->post("limit"); // 一页显示几行
            $limit = empty($limit) ? 10 :  $limit;
            $page = empty($offset) ? 0 :  bcmul($offset,$limit);

            $whereSql['share_goods_reward.status'] = 1;

            $maps = [];
            $map = json_decode($this->request->get('filter'), true);
            if(!empty($map)){
                foreach ($map as $k=>$v){
                    if($k = 'real_name'){
                        $maps['shopro_user_bank.real_name'] = $v;
                    }else if($k = 'card_no'){
                        $maps['shopro_user_bank.card_no'] = $v;
                    }else if($k = 'bank_name'){
                        $maps['shopro_user_bank.bank_name'] = $v;
                    }
                }
            }

            $count = Db::table('share_goods_reward')->join('shopro_user_bank', 'share_goods_reward.user_id=shopro_user_bank.user_id', 'left')->where($whereSql)->where($maps)->count();

            $list = Db::table('share_goods_reward')->join('shopro_user_bank', 'share_goods_reward.user_id=shopro_user_bank.user_id', 'left')
                ->field('share_goods_reward.id,share_goods_reward.money,share_goods_reward.month,share_goods_reward.addtime,shopro_user_bank.real_name,shopro_user_bank.card_no,shopro_user_bank.bank_name')
                ->where($whereSql)->where($maps)
                ->order('share_goods_reward.addtime', 'DESC')
                ->limit($page,$limit)->select();

            $result = array("total" => $count, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
        
    }




}
